﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DemoFacebookAPI
{
    public partial class frmMain : Form
    {
        Owner owner;
        private frmLogin frmLogin;
        public frmMain(frmLogin frmLogin)
        {
            InitializeComponent();
            this.frmLogin = frmLogin;
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            //
            btnPost.Location = new Point(308, 122);
            //
            lblLinkPost.Hide();
            txtLink.Hide();
            //
            lblPicPost.Hide();
            txtPicture.Hide();
            btnBrowserForPicture.Hide();
            //
            chkNone.Checked = true;

            owner = FBHelper.GetUser(AppSettings.Default.AccessToken);
            if (owner != null)
            {
                lblName.Text = owner.Name;
                lblSchool.Text = owner.School;
                lblLive.Text = owner.Live;
                lblLink.Text = owner.Link;
            }
            else
                MessageBox.Show("Ko lay duoc User");

            string imgURL = String.Format("http://graph.facebook.com/{0}/picture", FBHelper.GetUserID(AppSettings.Default.AccessToken));
            imgAccount.ImageLocation = imgURL;
            
        }

        private void btnChat_Click(object sender, EventArgs e)
        {
            String strNameChatFace = (owner.Link).Substring((owner.Link).LastIndexOf('/') + 1);
            RefreshEngine();
            Console.WriteLine(Engine.contact_Connected+"main");
            frmFriendsList frm = new frmFriendsList(strNameChatFace,this,btnChat);
            //this.Hide();
            btnChat.Enabled = false;
            frm.Show();
            
        }

        public void RefreshEngine()
        {
            Engine.facebookNick = "";
            Engine.facebookPass = "";
            Engine.Loading = false;
            Engine.logging = false;
            Engine.logged = false;
            Engine.chatRunning = false;
            Engine.loggingFailed = false;
            Engine.JID_Receiver = new List<string>();
            Engine.contact_Connected = 0;
            Engine.contact_Disconnected = 0;

            //second thread
            Engine.temp_JID_Receiver = "";
            Engine.SendMessage = false;
            Engine.SendMessageBody = "";
        }

        private void chkAttLink_CheckedChanged(object sender, EventArgs e)
        {
            lblLinkPost.Show();
            txtLink.Show();
            btnPost.Location = new Point(308, 143);
            //
            lblPicPost.Hide();
            txtPicture.Hide();
            btnBrowserForPicture.Hide();
            //
            chkAttPicture.Checked = false;
            chkNone.Checked = false;
        }

        private void chkAttPicture_CheckedChanged(object sender, EventArgs e)
        {
            lblPicPost.Show();
            txtPicture.Show();
            btnBrowserForPicture.Show();
            btnPost.Location = new Point(308, 143);
            //
            lblLinkPost.Hide();
            txtLink.Hide();
            //
            chkNone.Checked = false;
            chkAttLink.Checked = false;
        }

        private void chkNone_CheckedChanged(object sender, EventArgs e)
        {
            chkAttLink.Checked = false;
            chkAttPicture.Checked = false;
            //
            //
            btnPost.Location = new Point(308, 122);
            //
            lblLinkPost.Hide();
            txtLink.Hide();
            //
            lblPicPost.Hide();
            txtPicture.Hide();
            btnBrowserForPicture.Hide();
        }

        private void btnBrowserForPicture_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            opf.Filter = "JPG Image|*.jpg|PNG Images|*.png";
            if (opf.ShowDialog() == System.Windows.Forms.DialogResult.OK && opf.FileName != string.Empty)
            {
                txtPicture.Text = opf.FileName;
            }
        }

        private void btnPost_Click(object sender, EventArgs e)
        {
            if (txtLink.Visible == true)
            {
                if (FBHelper.Post(AppSettings.Default.AccessToken, txtStatus.Text, txtLink.Text))
                {
                    MessageBox.Show("Sent a link!");
                }
            }
            else
                if (txtPicture.Visible)
                {
                    if (FBHelper.PostImage(AppSettings.Default.AccessToken, txtStatus.Text, txtPicture.Text))
                    {
                        MessageBox.Show("sent Picture!");
                    }
                }
                else
                    if (FBHelper.PostStatus(AppSettings.Default.AccessToken, txtStatus.Text))
                    {
                        MessageBox.Show("Sent Status1");
                    }
        }

        private void chkAttLink_Click(object sender, EventArgs e)
        {
            chkAttLink.Checked = true;
            chkAttPicture.Checked = false;
            chkNone.Checked = false;
        }

        private void chkAttPicture_Click(object sender, EventArgs e)
        {
            chkAttLink.Checked = false;
            chkAttPicture.Checked = true;
            chkNone.Checked = false;
        }

        private void chkNone_Click(object sender, EventArgs e)
        {
            chkAttLink.Checked = false;
            chkAttPicture.Checked = false;
            chkNone.Checked = true;
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmLogin.Close();
        }
    }
}
